package com.mj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

@Controller
public class Hello {
    @RequestMapping("/test") @ResponseBody
    public void test(){
        System.out.println("hello 框架");
    }
    @RequestMapping("/user.action") @ResponseBody
    public void user(HttpServletRequest request,HttpServletResponse response){
        //HttpServletRequest request = null;
        //HttpServletResponse response = null;
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("你好色彩");
            out.flush();
            out.close();
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/info")
    public ModelAndView info(ModelAndView mv){
        mv.setViewName("info");//根目录下info.jsp为视图文件
        mv.addObject("name","上上谦");
        mv.addObject("address","上海市虹口区虹口足球场");
        return mv;
    }
    @RequestMapping("/ok")//如果里面不写则表示默认，如果没有index.jsp等首页文则会直接找到该页面
    public String ok(Model m){
        m.addAttribute("name","欧克man");
        return "ok";// 根目录下的 ok.jsp 视图文件
    }
}
